<?php

namespace App\Http\Controllers\Api\Category;

use App\Enums\Api\Response;
use App\Http\Controllers\Controller;
use App\Models\Category\Category;
use App\Models\Category\CategoryModule;
use App\Models\Tool\FileUrl;
use Illuminate\Database\Eloquent\Builder;

class CategoryController extends Controller
{
    public function index()
    {
        $category = Category::query()->has('category_module')->with('category_module:id,category_id,type,name,module_type,ui,is_open')->whereHas('category_module', function (Builder $builder){
            $builder->where('is_open', CategoryModule::isOpenOn);
        })->get();
        $file_url=FileUrl::query()->where('module',CategoryModule::ModuleTypeTen)->first();
        foreach ($category as &$item) {
            $item->image=convert_file_address_exist(module:CategoryModule::ModuleTypeTen,file_address:$item->image,grade:$item->id,category:'',type: '.png',file_url: $file_url);
        }
        return apiResponse(code:Response::SERVICE_SUCCESS->value,data:$category,msg: Response::SERVICE_SUCCESS->response(),type: 'category');
    }
}
